/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package media_server;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 *
 * @author Maytinh
 */
public class CreateXmlFile_Kenh2 {
     public static Document document=null;
     public Document getDocument()
     {
        return document;
     }
     public CreateXmlFile_Kenh2(String S_port,String S_Link,String S_Link_id)
     {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try
        {
          DocumentBuilder builder = factory.newDocumentBuilder();
          document = builder.newDocument();
        }
        catch (ParserConfigurationException parserException)
        {
        }
         Element root = document.createElement("Kenh2");
          // create attribute
        Attr genderAttribute = document.createAttribute("Port");
        genderAttribute.setValue(S_port);
         // append attribute to contact element
        root.setAttributeNode(genderAttribute);

         document.appendChild(root);


        // add comment to XML document
        Comment simpleComment = document.createComment("Danh sach duong dan den file video");
        root.appendChild(simpleComment);

        // add child element
        Node contactNode = createContactNode(document,S_Link,S_Link_id);
        root.appendChild(contactNode);


        // write the XML document to disk
        try {

          // create DOMSource for source XML document
          Source xmlSource = new DOMSource(document);

          // create StreamResult for transformation result
          Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

          // create TransformerFactory
          TransformerFactory transformerFactory = TransformerFactory.newInstance();

          // create Transformer for transformation
          Transformer transformer = transformerFactory.newTransformer();

          transformer.setOutputProperty("indent", "yes");

          // transform and deliver content to client
          transformer.transform(xmlSource, result);
        }

        // handle exception creating TransformerFactory
        catch (TransformerFactoryConfigurationError factoryError) {
          System.err.println("Error creating " + "TransformerFactory");
        }catch (TransformerException transformerError) {
          System.err.println("Error transforming document");
        }    catch (IOException ioException) {
        }
    }
      public CreateXmlFile_Kenh2(String S_port)
     {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try
        {
          DocumentBuilder builder = factory.newDocumentBuilder();
          document = builder.newDocument();
        }
        catch (ParserConfigurationException parserException)
        {
        }
         Element root = document.createElement("Kenh2");
          Attr genderAttribute = document.createAttribute("Port");
        genderAttribute.setValue(S_port);
         // append attribute to contact element
        root.setAttributeNode(genderAttribute);
        document.appendChild(root);




        // write the XML document to disk
        try {

          // create DOMSource for source XML document
          Source xmlSource = new DOMSource(document);

          // create StreamResult for transformation result
          Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

          // create TransformerFactory
          TransformerFactory transformerFactory = TransformerFactory.newInstance();

          // create Transformer for transformation
          Transformer transformer = transformerFactory.newTransformer();

          transformer.setOutputProperty("indent", "yes");

          // transform and deliver content to client
          transformer.transform(xmlSource, result);
        }

        // handle exception creating TransformerFactory
        catch (TransformerFactoryConfigurationError factoryError) {
          System.err.println("Error creating " + "TransformerFactory");
        }catch (TransformerException transformerError) {
          System.err.println("Error transforming document");
        }    catch (IOException ioException) {
        }
    }
     public static Node createContactNode(Document document,String S_Link,String S_Link_id)
     {

        // create FirstName and LastName elements
        Element link = document.createElement("Link");
         // create attribute
        Attr genderAttribute1 = document.createAttribute("Id");
        genderAttribute1.setValue(S_Link_id);
         // append attribute to contact element
        link.setAttributeNode(genderAttribute1);

        link.appendChild(document.createTextNode(S_Link));

        return link;
  }


    String searchFor;
//    public static void AddNodeDocument(String uri,Document document,String S_port,String S_Link_id,String S_Link)
//    {
//        //searchFor="Mails";
//
//        int n=0;
//        DOMParser parer = new DOMParser();
//        try
//        {
//            parer.parse(uri);
//            document=(Document)parer.getDocument();
//
//            NodeList node=document.getElementsByTagName("Kenh2");
//        //   NodeList root=document.getElementsByTagName("Kenh2");
//            if(node!=null)
//            {
//                for(int i=0;i<node.getLength();i++)
//                {
//                    if(S_port!=null)
//                    {
//                         if(node.item(i).getAttributes().item(0).getNodeValue().equals(S_port))
//                         {
//
//                            Element Link = document.createElement("Link");
//                            Attr id= document.createAttribute("Id");
//                            id.setValue(S_Link_id);
//                            Link.setAttributeNode(id);
//                            Link.appendChild(document.createTextNode(S_Link));
//
//                            node.item(i).appendChild(Link);
//
//                         }
//                         else
//                         {
//                             n++;
//                         }
//                          // write the XML document to disk
//                        try
//                        {
//
//                          // create DOMSource for source XML document
//                          Source xmlSource = new DOMSource(document);
//
//                          // create StreamResult for transformation result
//                          Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));
//
//                          // create TransformerFactory
//                          TransformerFactory transformerFactory = TransformerFactory.newInstance();
//
//                          // create Transformer for transformation
//                          Transformer transformer = transformerFactory.newTransformer();
//
//                          transformer.setOutputProperty("indent", "yes");
//
//                          // transform and deliver content to client
//                          transformer.transform(xmlSource, result);
//                        }
//
//                        // handle exception creating TransformerFactory
//                        catch (TransformerFactoryConfigurationError factoryError)
//                        {
//                          System.err.println("Error creating " + "TransformerFactory");
//
//                        }
//                        catch (TransformerException transformerError)
//                        {
//                          System.err.println("Error transforming document");
//
//                        }
//                        catch (IOException ioException)
//                        {
//
//                        }
//
//                    }
//                }
//                 if(n==node.getLength())
//                 {
//
//                      // create FirstName and LastName elements
//                        Element link = document.createElement("Link");
//                        Attr id= document.createAttribute("Id");
//
//                        id.setValue(S_Link_id);
//                        link.setAttributeNode(id);
//                        link.appendChild(document.createTextNode(S_Link));
//
//
//                        // create contact element
//                        Element contact = document.createElement("Kenh2");
//
//                        // create attribute
//                        Attr genderAttribute = document.createAttribute("Port");
//                        genderAttribute.setValue(S_port);
//                         // append attribute to contact element
//                        contact.setAttributeNode(genderAttribute);
//                        contact.appendChild(link);
//                        document.appendChild(contact);
//
//                        // append attribute to contact element
//                       // contact.setAttributeNode(genderAttribute);
//
//
////                        node.appendChild(contact);
////                         node.appendChild(link);
//                         // write the XML document to disk
//                        try
//                        {
//
//                          // create DOMSource for source XML document
//                          Source xmlSource = new DOMSource(document);
//
//                          // create StreamResult for transformation result
//                          Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));
//
//                          // create TransformerFactory
//                          TransformerFactory transformerFactory = TransformerFactory.newInstance();
//
//                          // create Transformer for transformation
//                          Transformer transformer = transformerFactory.newTransformer();
//
//                          transformer.setOutputProperty("indent", "yes");
//
//                          // transform and deliver content to client
//                          transformer.transform(xmlSource, result);
//                        }
//
//                        // handle exception creating TransformerFactory
//                        catch (TransformerFactoryConfigurationError factoryError)
//                        {
//                          System.err.println("Error creating " + "TransformerFactory");
//
//                        }
//                        catch (TransformerException transformerError)
//                        {
//                          System.err.println("Error transforming document");
//                        }
//                        catch (IOException ioException)
//                        {
//                        }
//
//
//                 }
//
//
//            }
//
//        }
//        catch (SAXException ex)
//        {
//           // Logger.getLogger(Frm.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        catch (IOException ex)
//        {
//           // Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//    }
    public static int timViTriNode(String vt_S,String id_port)
    {
        DOMParser parer = new DOMParser();
        try {
            parer.parse("Kenh2.xml");
        } catch (SAXException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
            document=(Document)parer.getDocument();
            NodeList n= document.getElementsByTagName("Link");
            int vt=0;
            for(int i=0;i<n.getLength(); i++)
            {
                if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().toLowerCase().equals(id_port))
                {


                   if(n.item(i).getAttributes().item(0).getNodeValue().equals(vt_S))
                   {
                        return vt;

                    }
                   vt++;
                }
            }
            return -1;
    }
     public static void AddThuocTinhAddNodeDocument(String uri,Document document,String S_id_Link,String S_id,int vt)
    {

        DOMParser parer = new DOMParser();
        try
        {
            parer.parse(uri);
            document=(Document)parer.getDocument();
            NodeList n= document.getElementsByTagName("Link");
            int k=-1;
            for(int i=0;i<n.getLength(); i++)
            {

                if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().equals(S_id))
                {
                  k++;
                  if(k==vt)
                  {
                    Attr genderAttribute = document.createAttribute("Readed");
                    genderAttribute.setValue(S_id_Link);
                     // append attribute to contact element
                    Element e =(Element)n.item(i);
                    e.setAttributeNode(genderAttribute);

                    // write the XML document to disk
                    try
                    {

                      // create DOMSource for source XML document
                      Source xmlSource = new DOMSource(document);

                      // create StreamResult for transformation result
                      Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

                      // create TransformerFactory
                      TransformerFactory transformerFactory = TransformerFactory.newInstance();

                      // create Transformer for transformation
                      Transformer transformer = transformerFactory.newTransformer();

                      transformer.setOutputProperty("indent", "yes");

                      // transform and deliver content to client
                      transformer.transform(xmlSource, result);
                    }

                    // handle exception creating TransformerFactory
                    catch (TransformerFactoryConfigurationError factoryError)
                    {
                      System.err.println("Error creating " + "TransformerFactory");
                    }
                    catch (TransformerException transformerError)
                    {
                      System.err.println("Error transforming document");
                    }
                    catch (IOException ioException)
                    {
                    }
                }
             }

          }
        }
        catch (SAXException ex)
        {
           // Logger.getLogger(Frm.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
           // Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     public static boolean kiemTraID(String id_port,String id_link)
    {
        DOMParser parer = new DOMParser();
        try {
            parer.parse("Kenh2.xml");
        } catch (SAXException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
            document=(Document)parer.getDocument();
            NodeList n= document.getElementsByTagName("Link");

            for(int i=0;i<n.getLength(); i++)
            {
                if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().toLowerCase().equals(id_port))
                {
                    if(n.item(i).getAttributes().item(1)!=null)
                   {
                        if(n.item(i).getAttributes().item(1).getNodeValue().equals(id_link))
                        {
                           return true;
                        }
                    }
}
            }
         return false;
     }
     public static int SoThuMoi(String id_port)
    {
         int sothu=0;
          DOMParser parer = new DOMParser();
        try {
            parer.parse("Kenh2.xml");
        } catch (SAXException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
            document=(Document)parer.getDocument();
            NodeList n= document.getElementsByTagName("Link");

            for(int i=0;i<n.getLength(); i++)
            {
                if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().equals(id_port))
                {
                    if(n.item(i).getAttributes().item(1)==null)
                    {
                        sothu ++;
                    }
                }
            }
         return sothu;
     }

     public static boolean kiemTraTonTai(String id_port, String id_link)
    {
         DOMParser parer = new DOMParser();
        try {
            parer.parse("Kenh2.xml");
        } catch (SAXException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
            document=(Document)parer.getDocument();
            NodeList n= document.getElementsByTagName("Link");

            for(int i=0;i<n.getLength(); i++)
            {
                if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().equals(id_port))
                {

                    if(n.item(i).getAttributes().item(0).getNodeValue().equals(id_link) )
                    {
                        return true;
                    }
                }
            }
         return false;
     }
     public static void xoa(String id_port,String id_link)
    {
         DOMParser parer = new DOMParser();
        try
        {
            parer.parse("Kenh2.xml");
        } catch (SAXException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
        document=(Document)parer.getDocument();
        NodeList n= document.getElementsByTagName("Link");
         for(int i=0;i<n.getLength(); i++)
        {
            if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().equals(id_port))
            {
                if(n.item(i).getAttributes().item(0).getNodeValue().equals(id_link) )
                {
                    for(int j=0;j<n.item(i).getChildNodes().getLength();j++)
                    {

                        Node e=n.item(i).getChildNodes().item(j);
                        if(e.getNodeType()==e.ELEMENT_NODE)
                        {
                            e.getParentNode().removeChild(e);
                        }
                      //System.out.println(e.getNodeName());
                         try {

                              // create DOMSource for source XML document
                              Source xmlSource = new DOMSource(document);

                              // create StreamResult for transformation result
                              Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

                              // create TransformerFactory
                              TransformerFactory transformerFactory = TransformerFactory.newInstance();

                              // create Transformer for transformation
                              Transformer transformer = transformerFactory.newTransformer();

                              transformer.setOutputProperty("indent", "yes");

                              // transform and deliver content to client
                              transformer.transform(xmlSource, result);
                            }

                            // handle exception creating TransformerFactory
                            catch (TransformerFactoryConfigurationError factoryError)
                            {
                              System.err.println("Error creating " + "TransformerFactory");
                            }
                            catch (TransformerException transformerError)
                            {
                              System.err.println("Error transforming document");
                            }
                            catch (IOException ioException)
                            {
                            }
                    }
                }
            }
        }
    }

     public static void xoaTatCa(String id_port,String id_link)
    {
         DOMParser parer = new DOMParser();
        try
        {
            parer.parse("Kenh2.xml");
        }
        catch (SAXException ex)
        {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(CreateXmlFile_Kenh2.class.getName()).log(Level.SEVERE, null, ex);
        }
        document=(Document)parer.getDocument();
        NodeList n= document.getElementsByTagName("Link");
         System.out.println(n.getLength());
        for(int i=0;i<n.getLength(); i++)
        {
            if(n.item(i).getParentNode().getAttributes().item(0).getNodeValue().equals(id_port))
            {
                Node e=n.item(i);
               e.getParentNode().removeChild(e);
              //System.out.println(e.getNodeName());
            }
        }
        try
        {

              // create DOMSource for source XML document
              Source xmlSource = new DOMSource(document);

              // create StreamResult for transformation result
              Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

              // create TransformerFactory
              TransformerFactory transformerFactory = TransformerFactory.newInstance();

              // create Transformer for transformation
              Transformer transformer = transformerFactory.newTransformer();

              transformer.setOutputProperty("indent", "yes");

              // transform and deliver content to client
              transformer.transform(xmlSource, result);
         }

        // handle exception creating TransformerFactory
        catch (TransformerFactoryConfigurationError factoryError)
        {
          System.err.println("Error creating " + "TransformerFactory");
        }
        catch (TransformerException transformerError)
        {
          System.err.println("Error transforming document");
        }
        catch (IOException ioException)
        {
        }

     }
       public static void AddNodeDocument(String uri,Document document,String S_port,String S_Link_id,String S_Link)
    {
        //searchFor="Mails";

        int n=0;
        DOMParser parer = new DOMParser();
        try
        {
            parer.parse(uri);
            document=(Document)parer.getDocument();

            NodeList node=document.getElementsByTagName("Kenh2");
        //   NodeList root=document.getElementsByTagName("Kenh2");
            if(node!=null)
            {
                for(int i=0;i<node.getLength();i++)
                {
                    if(S_port!=null)
                    {
                         if(node.item(i).getAttributes().item(0).getNodeValue().equals(S_port))
                         {

                            Element Link = document.createElement("Link");
                            Attr id= document.createAttribute("Id");
                            id.setValue(S_Link_id);
                            Link.setAttributeNode(id);
                            Link.appendChild(document.createTextNode(S_Link));

                            node.item(i).appendChild(Link);

                         }

                          // write the XML document to disk
                        try
                        {

                          // create DOMSource for source XML document
                          Source xmlSource = new DOMSource(document);

                          // create StreamResult for transformation result
                          Result result = new StreamResult(new FileOutputStream("Kenh2.xml"));

                          // create TransformerFactory
                          TransformerFactory transformerFactory = TransformerFactory.newInstance();

                          // create Transformer for transformation
                          Transformer transformer = transformerFactory.newTransformer();

                          transformer.setOutputProperty("indent", "yes");

                          // transform and deliver content to client
                          transformer.transform(xmlSource, result);
                        }

                        // handle exception creating TransformerFactory
                        catch (TransformerFactoryConfigurationError factoryError)
                        {
                          System.err.println("Error creating " + "TransformerFactory");

                        }
                        catch (TransformerException transformerError)
                        {
                          System.err.println("Error transforming document");

                        }
                        catch (IOException ioException)
                        {

                        }

                    }
                }
                


            }

        }
        catch (SAXException ex)
        {
           // Logger.getLogger(Frm.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
           // Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
